﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI
{
    public partial class MainGUI : Form
    {
        System.Windows.Forms.UserControl _uctrl;
        int _userID; // current user

        public MainGUI()
        {
            InitializeComponent();
            _uctrl = null;
            _userID = GUI.gui_dto.User.INVALID_USER_ID;
        }

        private void Load_User_Control(System.Windows.Forms.UserControl uc)
        {
            if (uc != null) 
            {
                _uctrl = uc;
                panel_Main.Controls.Clear();
                panel_Main.Controls.Add(_uctrl);
                /*
                 * set title
                 */
                this.Text = ((GUI.uc_control.ucInterface)_uctrl).GetName();
                /*
                 * set menu
                 */
                this.menu_Main.Items.Clear();
                if (_userID == GUI.gui_dto.User.INVALID_USER_ID) // this user not login so only have exit
                {
                    System.Windows.Forms.ToolStripMenuItem ToolStrip_FileMenu =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_FileMenu.Text = "File";
                    this.menu_Main.Items.AddRange(
                            new System.Windows.Forms.ToolStripItem[] {
                                ToolStrip_FileMenu
                            }
                        );
                    System.Windows.Forms.ToolStripMenuItem ToolStrip_Exit =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_Exit.Text = "Exit";
                    ToolStrip_Exit.Click += new EventHandler(ToolStrip_Exit_Click);
                    ToolStrip_FileMenu.DropDownItems.AddRange(
                            new System.Windows.Forms.ToolStripItem[] { ToolStrip_Exit }
                        );
                }
                else // get menu for this user
                {
                    System.Windows.Forms.ToolStripMenuItem ToolStrip_FileMenu =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_FileMenu.Text = "File";

                    System.Windows.Forms.ToolStripMenuItem ToolStrip_ToolsMenu =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_ToolsMenu.Text = "Tools";

                    this.menu_Main.Items.AddRange(
                            new System.Windows.Forms.ToolStripItem[] {
                                ToolStrip_FileMenu, ToolStrip_ToolsMenu
                            }
                        );

                    System.Windows.Forms.ToolStripMenuItem ToolStrip_Exit =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_Exit.Text = "Exit";
                    ToolStrip_Exit.Click += new EventHandler(ToolStrip_Exit_Click);

                    System.Windows.Forms.ToolStripMenuItem ToolStrip_Print =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_Print.Text = "Print";

                    System.Windows.Forms.ToolStripMenuItem ToolStrip_QuanLyNhanVien =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_QuanLyNhanVien.Text = "Quan Ly Nhan Vien";
                    ToolStrip_QuanLyNhanVien.Click += new EventHandler(ToolStrip_QuanLyNhanVien_Click);

                    System.Windows.Forms.ToolStripMenuItem ToolStrip_ChamCongNhanVien =
                        new System.Windows.Forms.ToolStripMenuItem();
                    ToolStrip_ChamCongNhanVien.Text = "Cham Cong Nhan Vien";
                    ToolStrip_ChamCongNhanVien.Click += new EventHandler(ToolStrip_ChamCongNhanVien_Click);

                    ToolStrip_FileMenu.DropDownItems.AddRange(
                            new System.Windows.Forms.ToolStripItem[] { ToolStrip_Print, ToolStrip_Exit }
                        );

                    ToolStrip_ToolsMenu.DropDownItems.AddRange(
                            new System.Windows.Forms.ToolStripItem[] { ToolStrip_QuanLyNhanVien, 
                                ToolStrip_ChamCongNhanVien }
                        );
                }
            }
        }

        private void MainGUI_Load(object sender, EventArgs e)
        {
            /*
             * load login control first
             */
            Load_User_Control(new GUI.uc_control.Login(Process_UCEvent));
        }

        /*
         * Process event for each user control that show in this GUI
         */
        private void Process_UCEvent(GUI.uc_control.ucEventInterface ev)
        {
            System.Windows.Forms.UserControl uc = null;

            switch (ev.GetEventID())
            {
                case GUI.uc_control.uc_event.ucEventID.LOGIN_EVENT_ID:
                    _userID = ((GUI.uc_control.uc_event.ucLoginEvent)ev).getUserID();
                    uc = Get_UserControl(
                            Get_Default_GUIID(
                                _userID
                            )
                        );
                    break;
                default:
                    break;
            }

            Load_User_Control(uc);
        }

        /*
         * Get default gui for each user, can be config by user
         */
        private int Get_Default_GUIID(int userID)
        {
            if (userID == 1) // admin
                return GUI.uc_control.ucID.UCID_QUAN_LY_NHAN_VIEN;
            return GUI.uc_control.ucID.UCID_INVALID;
        }

        /*
         * Get user control for each gui by user control ID
         */
        private System.Windows.Forms.UserControl Get_UserControl(int ucid)
        {
            switch (ucid)
            {
                case GUI.uc_control.ucID.UCID_QUAN_LY_NHAN_VIEN:
                    return new GUI.uc_control.QuanLyNhanVien(Process_UCEvent);
                case GUI.uc_control.ucID.UCID_CHAM_CONG_NHAN_VIEN:
                    return new GUI.uc_control.ChamCongNhanVien(Process_UCEvent);
                case GUI.uc_control.ucID.UCID_LOGIN:
                    return new GUI.uc_control.Login(Process_UCEvent);
                default:
                    return null;
            }
        }

        private void ToolStrip_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ToolStrip_ChamCongNhanVien_Click(object sender, EventArgs e)
        {
            if (Perf_Change() == 0) // save ok
            {
                Load_User_Control(Get_UserControl(GUI.uc_control.ucID.UCID_CHAM_CONG_NHAN_VIEN));
            }
        }
        private void ToolStrip_QuanLyNhanVien_Click(object sender, EventArgs e)
        {
            if (Perf_Change() == 0) // save ok
            {
                Load_User_Control(Get_UserControl(GUI.uc_control.ucID.UCID_QUAN_LY_NHAN_VIEN));
            }
        }

        private int Perf_Exit()
        {
            if (
                  MessageBox.Show("BAN MUON THOAT CHUONG TRINH?", "CONFIRM EXIT", MessageBoxButtons.YesNo)
                  == DialogResult.Yes)
            {
                DoHouseKeeping();
                return 1;
            }
            return 0;
        }

        private int Perf_Change()
        {
            if (((GUI.uc_control.ucInterface)_uctrl).NeedToSave())
            {
                if (
                      MessageBox.Show("BAN CO MUON LUU NHUNG THAY DOI?", "CONFIRM CHANGE", MessageBoxButtons.YesNo)
                      == DialogResult.Yes)
                {
                    if (((GUI.uc_control.ucInterface)_uctrl).SaveChange() != 0)
                        return (-1); // save failed
                }
            }
            return 0;
        }

        private void DoHouseKeeping() { }
    }
}
